package com.chookss.home.audit.seal;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SealAuditAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Activity context;

    public SealAuditAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvStatus = helper.getView(R.id.tvStatus);
        ImageView ivType = helper.getView(R.id.ivType);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvTime2 = helper.getView(R.id.tvTime2);
        TextView tvNum = helper.getView(R.id.tvNum);

        TextView tvRemarkInit = helper.getView(R.id.tvRemarkInit);
        TextView tvRemark = helper.getView(R.id.tvRemark);
        LinearLayout llTime = helper.getView(R.id.llTime);
        LinearLayout llType = helper.getView(R.id.llType);
        LinearLayout llNum = helper.getView(R.id.llNum);
        tvTitle.setText(entity.getProjectName());
        tvTime2.setText("发起时间："+entity.getCreateTime());
        if (entity.isSelect()) {
            ivType.setBackgroundResource(R.drawable.icon_fx_select);
        } else {
            ivType.setBackgroundResource(R.drawable.shape_circle_b5b5b5_20);
        }
        if(!Utils.isNull(entity.getParams().getUseDetail())){
            tvRemarkInit.setText("刻章内容：");
            if (Utils.isNull(entity.getParams().getUseDetail())) {
                tvRemark.setText("无");
            } else {
                tvRemark.setText(entity.getParams().getUseDetail());
            }
            if (Utils.isNull(entity.getParams().getUseNum())) {
                llNum.setVisibility(View.GONE);
            } else {
                llNum.setVisibility(View.VISIBLE);
                tvNum.setText("刻章数量：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getUseNum())+"个");
            }
        }else{
            tvRemarkInit.setText("用章事由：");
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvRemark.setText("无");
            } else {
                tvRemark.setText(entity.getParams().getRemark());
            }
            if (Utils.isNull(entity.getParams().getUseTime())) {
                llTime.setVisibility(View.GONE);
            } else {
                llTime.setVisibility(View.VISIBLE);
                tvTime.setText("使用时间：" + entity.getParams().getUseTime());
            }
            if (Utils.isNull(entity.getParams().getUseNum())) {
                llNum.setVisibility(View.GONE);
            } else {
                llNum.setVisibility(View.VISIBLE);
                tvNum.setText("使用次数：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getUseNum())+"次");
            }
        }
        llType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> map = new HashMap<>();
                map.put("ClockInAudit_position", helper.getItemPosition() + "");
                EventBus.getDefault().post(new MyEvent(map));
            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent  = new Intent(context, SealDetailActivity.class);
                intent.putExtra("goodsCode", entity.getProjectCode());
                intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                intent.putExtra("data", entity);
                context.startActivity(intent);
            }
        });
    }
}
